/*
 * watch_data_packet.h
 *
 *  Created on: 30.06.2013
 *      Author: SpirTanol
 */

#ifndef WATCH_DATA_PACKET_H_
#define WATCH_DATA_PACKET_H_

#include "packets/data_packet.h"
#include "global.h"

class WatchDataPacket : public DataPacket
{
	enum Reg
	{
		R_TEMP_NOM,
		R_CRASH_CODE,
		R_AUTO_START,
		R_INPUT_STATE,
		R_OUTPUT_STATE,
		R_ENGINE_STATE,
		R_TEMP_STATE,
		R_WET_STATE,
		R_TEMP_RT100,
		R_CUR,
		R_VOL = R_CUR + 3,
		R_TIME = R_VOL + 3,

		R_WORK_TIME = R_TIME + 2,

		R_SIZE
	};

	ushort oData[R_SIZE];

public:
	enum Output
	{
		O_POWER,
		O_APPEND,
		O_GENERAL,
	};

	WatchDataPacket();
	WatchDataPacket(const WatchDataPacket *packet);
	~WatchDataPacket();

	WatchDataPacket *clone() const { return new WatchDataPacket(this); } // [virtual]

	inline int tempNom() const { return int(oData[R_TEMP_NOM]); }
	inline int crashCode() const { return int(oData[R_CRASH_CODE]); }
	inline bool autoStart() const { return (oData[R_AUTO_START] != 0); }
	inline bool inputState(int input) const { return (((oData[R_INPUT_STATE] >> input) & 1) != 0); }
	bool outputState(Output out) const;
	inline ushort outputReg() const { return oData[R_OUTPUT_STATE]; }
	inline bool engineIsWoork() const { return (oData[R_ENGINE_STATE] != 0); }
	inline int tempState() const { return int(oData[R_TEMP_STATE]); }
	inline int wetState() const { return int(oData[R_WET_STATE]); }
	inline int tempRT100() const { return int(short(oData[R_TEMP_RT100])); }
	inline uint cur(Phase phase) const { return uint(oData[R_CUR + phase]); }
	inline uint vol(Phase phase) const { return uint(oData[R_VOL + phase]); }
	inline uint time() const { return *(const uint*)(&oData[R_TIME]); }
	inline uint workTime() const { return uint(oData[R_WORK_TIME]); }

private:
	void init();

};

class WatchDataPacket80 : public WatchDataPacket
{
	uint oStartCount;

public:
	WatchDataPacket80() : WatchDataPacket() { init(); }
	WatchDataPacket80(const WatchDataPacket80 *packet);
	~WatchDataPacket80() {}

	WatchDataPacket80 *clone() const { return new WatchDataPacket80(this); } // [virtual]

	inline uint startCount() const { return oStartCount; }

private:
	void init() { addPart(Part(&oStartCount, 2, 171)); }

};

#endif /* WATCH_DATA_PACKET_H_ */
